package com.hdd.fmh.module.sys.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hdd.fmh.common.config.Global;
import com.hdd.fmh.common.redis.RedisMapper;
import com.hdd.fmh.common.response.ResultUtils;
import com.hdd.fmh.common.response.ResultVo;
import com.hdd.fmh.common.utils.IdGen;
import com.hdd.fmh.common.utils.JsonUtils;
import com.hdd.fmh.common.utils.MD5Utils;
import com.hdd.fmh.common.utils.StringUtils;
import com.hdd.fmh.module.sys.bean.User;
import com.hdd.fmh.module.sys.controller.dto.UserDto;
import com.hdd.fmh.module.sys.mapper.UserMapper;
import com.hdd.fmh.module.sys.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class UserServiceImpl  implements UserService {
    @Autowired
    UserMapper userMapper;

    @Autowired
    RedisMapper redisMapper;

    @Override
    public ResultVo<User> doLogin(String username, String passwrod) throws Exception {
        User user = new User();
        user.setName(username);
        User userDb = userMapper.selectByUser(user);

        if (StringUtils.isNullOrEmpty(userDb)) {
            log.info("用户不存在,{}=====>passwrod,{}",username,passwrod);
            return ResultUtils.fail("user.login.user.not");
        }

        //2判断密码
        String dbpass =  MD5Utils.stringToMD5(passwrod+userDb.getSalt());
        if (!userDb.getPassword().equals(dbpass)) {
            log.info("密码错误,{}=====>passwrod,{}",username,passwrod);
            return  ResultUtils.fail("user.login.pass.error");
        }

        // 用户登陆成功的token
        String token = IdGen.uuid();

        // 存放到redis
        redisMapper.setEx(token, JsonUtils.toJSon(userDb), Global.getConfigInteger("user.login.token.timeout"), TimeUnit.MINUTES);
        userDb.setPassword("");
        userDb.setSalt("");
        // 返回token结果
        userDb.setToken(token);
        return ResultUtils.success(userDb);
    }

    @Override
    @Transactional(rollbackFor= Throwable.class)
    public void save(User user) throws Exception {
        if(StringUtils.isNullOrEmpty(user.getId())){//新增
            // 获取名称 进行查询是否存在
            User userDb = userMapper.selectByUser(user);
            if(!StringUtils.isNullOrEmpty(userDb)){
                throw  new Exception("用户已经存在");
            }
            // 1 随机生成  salt
            String salt =  StringUtils.getRandomStringByLength(5);
            user.setSalt(salt);

            String passb = user.getPassword();
            if(StringUtils.isBlank(passb)) {
                passb = "123456";
            }

            String pass =  MD5Utils.stringToMD5(passb+salt);
            user.setPassword(pass);
            user.setDelFlag(0);
            user.preInsert();
            userMapper.insertSelective(user);
        }else {//修改
//            user.setDelFlag("0");
            user.preUpdate();
            userMapper.updateByPrimaryKeySelective(user);
        }
    }


    public ResultVo<PageInfo<User>> page(UserDto dto){
        PageHelper.startPage(0,5);
        List<User> list = userMapper.findAll(dto);

        //这里可以进进行  循环  关联数据操作

        PageInfo<User> pageInfo = new PageInfo<>(list);
       return ResultUtils.success(pageInfo);
    }

    @Override
    public ResultVo<?> changPassWord(String uid, String oldPass) {


        return null;
    }

}
